package com.atguigu.atcrowdfunding.manager.service.impl;

import com.atguigu.atcrowdfunding.bean.Role;
import com.atguigu.atcrowdfunding.manager.dao.RoleMapper;
import com.atguigu.atcrowdfunding.manager.service.RoleService;
import com.atguigu.atcrowdfunding.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author xzt
 * @create 2021-02-26 16:36
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;


    @Override
    public Page queryPage(Map<String, Object> paramMap) {
        Page page = new Page((Integer) paramMap.get("pageNo"),(Integer) paramMap.get("pageSize"));

        Integer startIndex = page.startIndex();

        paramMap.put("startIndex",startIndex);

        List<Role> datas = roleMapper.queryPage(paramMap);

        page.setDatas(datas);
        Integer totalSize = roleMapper.queryCount(paramMap);

        page.setTotalSize(totalSize);

        return page;
    }

    @Override
    public int saveRolePermissionRelationship(Integer roleId, Integer[] ids) {
        int i = roleMapper.deleteByRoleId(roleId);
        int count =  roleMapper.saveRolePermissionRelationship(roleId, ids);

        return count;
    }

    @Override
    public int addRole(Role role) {
        return roleMapper.insert(role);
    }

    @Override
    public Role queryRoleById(Integer id) {
        return roleMapper.queryRoleById(id);
    }
}
